package com.surfilter.massdata.spark.model;


import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.hadoop.io.WritableComparable;
import org.apache.log4j.Logger;

import com.surfilter.massdata.spark.util.StringUtil;
import com.surfilter.massdata.spark.util.URLUtil;

public class StructDNS implements WritableComparable<StructDNS> {
	
	private static final Logger LOG = Logger.getLogger(StructDNS.class);
    private String cip;
    private String dip;
    private String domain;
    private String topDomain = null;
    private boolean updateTD = true;
    private int visitCount;
    private String visitTime;

    public StructDNS() {
    }

    public StructDNS(String text) {
        String[] tmp = text.split("\t");
        text = text.replaceAll("::", "");
        this.setCip(tmp[0])
                .setDip(tmp[2]).setDomain(tmp[1])
                .setVisitCount(Integer.valueOf(tmp[4]));
    }

    public StructDNS(String text, boolean simple) {
        String[] records = text.split(DnsStatConsts.FILE_SEP);
        setVisitCount(Integer.parseInt(records[1]));
        String[] items = records[0].split(StringUtil.STR_DELIMIT_1ST);
        setCip(items[0]);
        setDip(items[1]);
        setTopDomain(items[2]);
        updateTD = false;
    }

    public String getTopDomain() {
        if (updateTD) {
            try {
                topDomain = URLUtil.getDomainName("http://" + domain);
                updateTD = true;
            } catch (MalformedURLException e) {
            	LOG.error(e.getMessage(), e);
            }
        }
        return topDomain;
    }

    public StructDNS setTopDomain(String topDomain) {
        this.topDomain = topDomain;
        updateTD = false;
        return this;
    }

    public int getVisitCount() {
        return visitCount;
    }

    public StructDNS setVisitCount(int visitCount) {
        this.visitCount = visitCount;
        return this;
    }

    public String getCip() {
        return cip;
    }

    public StructDNS setCip(String cip) {
        this.cip = cip;
        return this;
    }

    public String getDip() {
        return dip;
    }

    public StructDNS setDip(String dip) {
        this.dip = dip;
        return this;
    }

    public String getDomain() {
        return domain;
    }

    public StructDNS setDomain(String domain) {
        this.domain = domain;
        updateTD = true;
        return this;
    }

    public void write(DataOutput out) throws IOException {
        out.writeUTF(cip);
        out.writeUTF(dip);
        out.writeUTF(domain);
        out.writeUTF(String.valueOf(visitCount));
    }

    public void readFields(DataInput in) throws IOException {
        cip = in.readUTF();
        dip = in.readUTF();
        domain = in.readUTF();
        visitCount = Integer.valueOf(in.readUTF());
    }

    public int compareTo(StructDNS o) {
        int ret = cip.compareTo(o.cip);
        if (ret == 0) {
            ret = dip.compareTo(o.dip);
            if (ret == 0) {
                ret = domain.compareTo(o.domain);
                if (ret == 0 && (visitCount == o.visitCount)) {
                    ret = 0;
                }
            }
        }
        return ret;
    }

    @Override
    public String toString() {
        return "StructDNS{" +
                "cip='" + cip + '\'' +
                ", dip='" + dip + '\'' +
                ", domain='" + domain + '\'' +
                ", topDomain='" + topDomain + '\'' +
                ", updateTD=" + updateTD +
                ", visitCount=" + visitCount +
                '}';
    }

	public void setVisitTime(String visitTime) {
		this.visitTime = visitTime;
	}

	public String getVisitTime() {
		return visitTime;
	}
}
